package com.fjh.springboot_01.controller;

import com.fjh.springboot_01.pojo.Friend;
import com.fjh.springboot_01.pojo.Result;
import com.fjh.springboot_01.service.FriendService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/friend")
public class FriendController {

    @Autowired
    private FriendService friendService;

    @Resource(name = "stringRedisTemplate")
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;


    @PostMapping("/add")
    public Result addFriendRequest(@RequestParam int userId, @RequestParam int friendId) {
        System.out.println("userId:  " + userId + ", friendId: " + friendId);
        Result result = friendService.addFriendRequest(userId, friendId);

        if (result.isSuccess()) {
            try {
                // 保存通知到数据库
                friendService.saveNotification(userId, friendId);
                
                // 如果用户在线，发送WebSocket通知
                String onlineStr = stringRedisTemplate.opsForValue().get("onlineStatus:" + friendId);
                if (onlineStr != null && onlineStr.equalsIgnoreCase("true")) {
                    String notification = "用户 " + userId + " 向您发送了好友请求";
                    simpMessagingTemplate.convertAndSendToUser(friendId + "", "/topic/notification", notification);
                }
            } catch (Exception e) {
                System.err.println("发送通知失败：" + e.getMessage());
            }
        }
        return result;
    }

    // 获取未处理的好友请求通知
    @GetMapping("/notifications")
    public Result getNotifications(@RequestParam int userId) {
        return friendService.getNotifications(userId);
    }

    // 接受好友请求
    @PostMapping("/accept")
    public Result acceptFriendRequest(@RequestParam int userId, @RequestParam int friendId) {
        return friendService.acceptFriendRequest(userId, friendId);
    }

    // 拒绝好友请求
    @PostMapping("/reject")
    public Result rejectFriendRequest(@RequestParam int userId, @RequestParam int friendId) {
        return friendService.rejectFriendRequest(userId, friendId);
    }

    // 获取好友列表
    @GetMapping("/list")
    public Result getFriendList(@RequestParam int userId) {
        return friendService.getFriendList(userId);
    }


    @DeleteMapping("/delete")
    public Result deleteFriend(@RequestParam int userId, @RequestParam int friendId) {
        return friendService.deleteFriend(userId,  friendId);
    }


    @PostMapping("/online")
    public Result setOnlineStatus(@RequestParam int userId, @RequestParam boolean online) {
        stringRedisTemplate.opsForValue().set("onlineStatus:"  + userId, online ? "true" : "false");
        return Result.success();
    }

    @GetMapping("/isOnline")
    public Result isOnline(@RequestParam int userId) {
        String onlineStr = stringRedisTemplate.opsForValue().get("onlineStatus:"  + userId);
        Boolean online = onlineStr != null && onlineStr.equalsIgnoreCase("true");
        return Result.success(online);
    }
}
